package org.yanfengying.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.yanfengying.org.domain.Department;
import org.yanfengying.org.query.DepartmentQuery;
import org.yanfengying.org.service.IDepartmentService;
import org.yanfengying.utils.AjaxResult;
import org.yanfengying.utils.PageList;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(description = "部门的相关操作")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //======增删查改=====
    @GetMapping("/list")
    @ApiOperation(value = "部门列表")
    public AjaxResult list(){
        try {
            List<Department> departments = departmentService.loadAll();
            return AjaxResult.me().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("查询失败").setSuccess(false);
        }
    }


    /**
     * 新增和修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "部门的新增和修改操作")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId() != null){
            departmentService.update(department);
            }
            departmentService.insert(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
        return AjaxResult.me();
    }

    /**
     * 删除操作
     * @PathVariable->地址栏中获取
     * @param id
     * @return
     */
    @ApiOperation(value = "部门的删除操作")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
        return AjaxResult.me();
    }

    /**|
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "部门根据id查询操作")
    public AjaxResult loadById(@PathVariable("id") Long id){
        try {
            Department department = departmentService.loadById(id);
        return AjaxResult.me().setResult(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "部门分页查询操作")
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            PageList pageList = departmentService.pageList(query);
            return new AjaxResult().me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "部门树")
    public AjaxResult tree(){
        try {
            List<Department> departments = departmentService.loadTreep();
            return new AjaxResult().me().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }






}
